package org.hxyjs.dao.impl;

import org.hxyjs.dao.TicketRecordDao;
import org.hxyjs.entity.TicketRecord;
import org.hxyjs.mapper.TicketRecordMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("ticketRecordDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class TicketRecordDaoImpl implements TicketRecordDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private TicketRecordMapper ticketRecordMapper;

    @Override
    public List<TicketRecord> getTicketRecordByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) ticketRecordMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return ticketRecordMapper.getTicketRecordByPage(map);
    }


    public List<TicketRecord> getAllTicketRecords() throws Exception {
        return ticketRecordMapper.selectByExample(null);
    }

    @Override
    public TicketRecord getTicketRecordByArcno(String arcno) throws Exception {
        return ticketRecordMapper.selectByPrimaryKey(arcno);
    }


    @Override
    public Integer deleteTicketRecordByArcno(String arcno) {
        return ticketRecordMapper.deleteByPrimaryKey(arcno);
    }

    @Override
    public Integer updateTicketRecordByArcno(TicketRecord ticketRecord) {

        return ticketRecordMapper.updateByPrimaryKey(ticketRecord);
    }

    @Override
    public Integer addTicketRecord(TicketRecord ticketRecord) {
        ticketRecord.setArcno(initId());
        return ticketRecordMapper.insertSelective(ticketRecord);
    }

    public String initId() {
        String maxArcno = ticketRecordMapper.getMaxArcno();
        return GenNumTools.initId("0", 3, maxArcno);
    }

}